﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using final_darskade.Models;

namespace final_darskade.Controllers
{
    public class CourseController : Controller
    {
        //
        // GET: /Course/
        private static darskadeEntities tablesEntities = new darskadeEntities();

        public ActionResult Index()
        {
            var courselist = from course in tablesEntities.courses
                             select course;
            return View("CourseList", courselist.ToList());
        }

        public ActionResult Create()
        {
            return View("Create");
        }

        [HttpPost]
        public ActionResult Create(course model)
        {
            bool exists = (from course in tablesEntities.courses
                         where course.course_id == model.course_id
                         select course).Any();
            if (exists)
            {
                ViewData["duplicateprimarykey"] = "<div class=\"formError course_idformError\" style=\"top: 282.733px; left: 827.45px; margin-top: 0px; opacity: 0.87;\"><div class=\"formErrorContent\">* This course id already exists.</div><div class=\"formErrorArrow\"><div class=\"line10\"><!-- --></div><div class=\"line9\"><!-- --></div><div class=\"line8\"><!-- --></div><div class=\"line7\"><!-- --></div><div class=\"line6\"><!-- --></div><div class=\"line5\"><!-- --></div><div class=\"line4\"><!-- --></div><div class=\"line3\"><!-- --></div><div class=\"line2\"><!-- --></div><div class=\"line1\"><!-- --></div></div></div>";
                return View();
            }
            else
            {
                tablesEntities.AddTocourses(model);
                tablesEntities.SaveChanges();
                return RedirectToAction("Index");
            }
        }

        [HttpGet]
        public ActionResult Edit(int course_id)
        {
            var model = from course in tablesEntities.courses
                        where course.course_id == course_id
                        select course;
            return View(model.First());
        }
        [HttpPost]
        public ActionResult Edit(course model)
        {
            tablesEntities.ApplyCurrentValues(tablesEntities.courses.EntitySet.Name, model);
            tablesEntities.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Delete(int course_id)
        {
            var model = from course in tablesEntities.courses
                        where course.course_id == course_id
                        select course;
            course c = model.First();
            tablesEntities.DeleteObject(c);
            tablesEntities.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult FullSearch(int? start, int? end, string name, string field, int? unit)
        {
            if (start == null) start = 0;
            if (end == null) end = int.MaxValue;
            var result = from course in tablesEntities.courses
                         where course.course_id >= start && course.course_id <= end
                                && course.name.Contains(name) && course.field.Contains(field)
                                && (unit == null || course.unit == unit)
                         select course;
            return View("CourseList", result.ToList());
        }
    }
}
